package components
{
	import SEApp.se_app;
	import SEApp.se_util;
	
	import UIFree.ObjectEvent;
	import UIFree.controls.base.uif_source;
	import UIFree.controls.uif_baseHint;
	import UIFree.controls.uif_close;
	import UIFree.controls.uif_hint;
	import UIFree.effects.other.oef_starsLayer;
	import UIFree.uif_component;
	
	import UIUtils.uif_util;
	
	import classes.clt_events;
	import classes.clt_loader;
	
	import flash.display.Shape;
	import flash.events.Event;
	import flash.events.MouseEvent;
	import flash.filters.DropShadowFilter;
	import flash.filters.GlowFilter;
	import flash.geom.Matrix;
	import flash.geom.Point;
	import flash.geom.Rectangle;
	
	public class clt_userReview extends uif_source {
		private var _user	: Object;
		private var _mask	: Shape;
		private var _effect	: oef_starsLayer;
		private var _moveTo	: Point;
		public function clt_userReview(parent:uif_component, size: int, styleClass:String='') {
			super(parent, 0, 0, size, size);
			buttonMode = true;
			source.alpha = 0.7;
		}
		
		override public function EnterHere(size: Point): Rectangle {
			return uif_util.EnterHere(getInner(), size, false);
		}
		
		override protected function listenEvents():void {
			super.listenEvents();
			addEvent(MouseEvent.ROLL_OVER, doOver);
			addEvent(MouseEvent.ROLL_OUT, doOut);
			addEvent(MouseEvent.CLICK, doClick);
			addEvent(MouseEvent.MOUSE_DOWN, doDown);
			addEvent(Event.CLOSE, doClose);
			addEvent(uif_component.ENDDRAG, doEndDrag);
		}
		
		private function doDown(e: MouseEvent): void {
			beginDrag(2);
		}
		
		private function doEndDrag(e: Event): void {
			_moveTo 	= position.clone();
		}
		
		private function doClick(e: MouseEvent): void {
			app.dispatchEvent(new ObjectEvent(clt_events.INVITEDIALOGSHOW, _user.uid));
			close();
		}
		
		private function doClose(e: Event): void {
			dispose();
		}
		
		private function doOver(e: Event): void {
			fx('fstate', fstate, 1).start();
		}
		
		private function doOut(e: Event): void {
			fx('fstate', fstate, 0).start();
		}
		
		public function setUser(a_user: Object): void {
			_user = a_user;
			loadSource(_user.pic, doLoadSource);
			hint = (clt.user.first_name?clt.user.first_name:se_util.userName(clt.user)) + clt.sitedata.REQUESTINVITE + se_util.userName(a_user);
		}
		
		protected function get clt(): clt_clothingBase {
			return app as clt_clothingBase;
		}
		
		private function doLoadSource(e: Event): void {
			var csize: Number = width / 2;
			_mask = new Shape();
			addChild(_mask);
			
			_mask.graphics.beginFill(0);
			_mask.graphics.drawCircle(csize, csize, csize);
			_mask.graphics.endFill();
			source.mask = _mask;
			source.filters = [
				new GlowFilter(0xFFFFFF, 0, 3, 3, 4, 3, true),
				new DropShadowFilter(3, 45, 0, 0.8, 8, 8, 1, 3)
			];
			
			_effect = new oef_starsLayer(This, getInner(), {
				maxRadius	: 5,
				alphaSpeed	: 0.02,
				tr			: [1,0,0,1,0,-.7]
			});
			_effect.x = _mask.x;
			_effect.y = _mask.y;
			
			position 	= new Point((parentComponent.width - width) * Math.random(), parentComponent.height);
			_moveTo 	= position.clone();
			
			addEvent(Event.ENTER_FRAME, doFrame);
//			new uif_close(This, width - 20, 1);
		}
		
		private function doFrame(e: Event): void {
			if (!dragState) {
				position = new Point(x + (_moveTo.x - x) / 5, y + (_moveTo.y - y) / 5);
				if (fstate < 0.5) {
					_moveTo.x += Math.sin(_moveTo.y * 0.1) * 5;
					_moveTo.y -= 2;//1 + Math.sin(_moveTo.y * 0.2) * 2;
				}
				if (y < -height) dispose();
			}
		}
		
		private function close(): void {
			slideHide(function(): void {
				dispose();
			});
		}
		
		public function get fstate(): Number {
			return source.filters[0].alpha * 2;
		}
		
		public function set fstate(a_value: Number): void {
			source.alpha = a_value * 0.3 + 0.7;
			var tmpFilters: Array = source.filters;
			tmpFilters[0].alpha = a_value  / 2;
			tmpFilters[0].blurX = a_value * 10;
			tmpFilters[0].blurY = a_value * 10;
			source.filters = tmpFilters;
		}
	}
}